// pages/supplementCard/supplementCard.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cardInfo: {}, //水卡信息
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getCardList(options.card, options.type);
  },



  /**
   * 获取水卡信息
   */
  getCardList: function (card, type) {
    var that = this;
    app.ajax({
      url: 'Partner/WaterCard/waterCardList',
      data: {
        page: 1,
        row: 999,
        card: card,
        type: type
      },
      success: function (res) {
        if (res.data.code == 1000) {
          //console.log(res.data.data.data);
          if (res.data.data.data.length > 0) {
            that.setData({
              cardInfo: res.data.data.data[0]
            })
          }
        }
      }
    })
  },

  // 确认
  formSubmit: function (e) {
    console.log(e.detail.value)
    var that = this;
    app.ajax({
      url: 'Partner/WaterCard/reissueCard',
      data: {
        water_id: that.data.cardInfo.water_id,
        sn: e.detail.value.realCard
      },
      success: function (res) {
        if (res.data.code == 1000) {
          //console.log(res.data.data.data);
          app.showToast(res.data.message);
          wx.navigateBack({
            delta: 1
          })
        } else {
          app.showToast(res.data.message);
        }
      }
    })
  },
  isNumber(val) {
    var regPos = /^\d+(\.\d+)?$/; //非负浮点数
    var regNeg = /^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/; //负浮点数
    if (regPos.test(val) || regNeg.test(val)) {
      return true;
    } else {
      return false;
    }
  },
  //扫描水卡
  scan: function () {
    wx.scanCode({
      success: (res) => {
        console.log("扫码结果");
        console.log(res.result);

        if (!this.isNumber(res.result)) {
          app.showToast("请扫描带有水卡卡号的二维码");
          return false;
        }
        this.setData({
          real_card: res.result
        })
      },
      fail: (res) => {
        console.log(res);
      }
    })
  },
})